<template>
  <h1>员工列表练习</h1>
  <input type="text" placeholder="输入员工姓名" v-model="list.name">
  <input type="text" placeholder="输入员工工资" v-model="list.salary">
  <input type="text" placeholder="输入员工年龄" v-model="list.age">
  <button @click="add()">添加</button>
  <br>
  <table border="2px">
    <tr>
      <th>编号</th>
      <th>姓名</th>
      <th>工资</th>
      <th>年龄</th>
    </tr>
    <tr v-for="(item,index) in list2">
      <td>{{ index + 1 }}</td>
      <td>{{ item.name }}</td>
      <td>{{ item.salary }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>
<script setup>
import {ref} from "vue";
//1.定义一个对象用来存储员工信息
const list = ref({name: '', salary: '', age: ''});
//2.定义一个数组用来存储多个员工信息,并且遍历显示出来
const list2 = ref([]);
//添加员工
const add = () => {
  // 1.判断输入框是否为空
  if (list.value.name.trim() === '' || list.value.salary.trim() === '' || list.value.age.trim() === '') {
    alert('请输入员工信息');
    return;
  }
  console.log(list.value)
  // 3.将员工信息添加到数组中
  list2.value.push({...list.value});
  // 清空输入框
  // list.value = { name: '', salary: '', age: '' };
  list.value = {};
}
</script>
<style scoped>

</style>